package com.david.service.Impl;

import com.david.dao.EmployeeMapper;
import com.david.entity.Employee;
import com.david.service.EmployeeInterface;
import com.david.vo.CardBean;
import com.david.vo.EmployeeBean;
import com.david.vo.EmployeeCondition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author David
 */
@Service
public class EmployeeService implements EmployeeInterface {

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public EmployeeBean getAllEmployee(Integer page, Integer limit){
        EmployeeBean employeeBean = new EmployeeBean();
        // 设置状态码
        employeeBean.setCode(0);
        employeeBean.setMsg("...");

        // 这儿开始分页，然后插入返回的数据
        PageHelper.startPage(page, limit);
        List<Employee> employeeList = employeeMapper.getAllEmployee();
        employeeBean.setData(employeeList);

        // 用PageInfo包装一下获取总行数
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        employeeBean.setCount((int)(pageInfo.getTotal()));

        return employeeBean;
    }

    @Override
    public CardBean saveEmployee(Employee employee){
        CardBean baseBean = new CardBean();
        // 这儿开始查找数据库中是否有此人
        if(employeeMapper.findEmployee(employee.getId()) != null)
        {
            baseBean.setCode(1);
            baseBean.setMsg("此ID已被占用");
        }else if(employeeMapper.findEmployeeByPhone(employee.getPhone()).size() != 0){
            // 根据电话来判断是否存在此人
            baseBean.setCode(1);
            baseBean.setMsg("此员工已存在");
        }else{
            // 这儿开始业务处理
            employeeMapper.save(employee);
            baseBean.setCode(0);
            baseBean.setMsg("员工登记成功");
        }
        return baseBean;
    }

    @Override
    public CardBean deleteEmployee(Integer id){
        CardBean baseBean = new CardBean();
        employeeMapper.deleteEmployee(id);
        baseBean.setMsg("删除成功！");
        baseBean.setCode(0);
        return baseBean;
    }

    @Override
    public CardBean batchDelete(Integer[] ids){
        CardBean baseBean = new CardBean();
        employeeMapper.batchDelete(ids);
        baseBean.setMsg("删除成功！");
        baseBean.setCode(0);
        return baseBean;
    }

    @Override
    public EmployeeBean<List<Employee>> findEmployeeByCondition(EmployeeCondition employeeCondition){
        EmployeeBean<List<Employee>> employeeBean = new EmployeeBean<>();
        // 设置状态码
        employeeBean.setCode(0);

        // 设置分页并获取总行数
        PageHelper.startPage(employeeCondition.getPage(), employeeCondition.getLimit());
        List<Employee> employeeList = employeeMapper.findEmployeeByCondition(employeeCondition);
        PageInfo<Employee> pageInfo = new PageInfo<>(employeeList);
        employeeBean.setCount((int)(pageInfo.getTotal()));

        // 存入数据
        employeeBean.setData(employeeList);

        if(employeeList.size() == 0){
            employeeBean.setMsg("不存在符合要求的员工！");
        }else{
            employeeBean.setMsg("查找成功！");
        }

        return employeeBean;
    }

    @Override
    public CardBean updateEmployee(Employee employee){
        CardBean baseBean = new CardBean();
        if(employeeMapper.findEmployee(employee.getId()) == null){
            baseBean.setCode(1);
            baseBean.setMsg("员工表中查无此人，请确认员工ID是否正确");
        }else if("".equals(employee.getName())){
            if("".equals(employee.getEmail()) && "".equals(employee.getPhone()) && "".equals(employee.getGender())){
                baseBean.setCode(1);
                baseBean.setMsg("请输入至少一条变更的信息");
            }
        } else{
            baseBean.setCode(0);
            baseBean.setMsg("员工信息更新成功！");
            employeeMapper.updateEmployee(employee);
        }
        return baseBean;
    }
}
